package ed.hbut.supermarket1.controller;

import ed.hbut.supermarket1.entity.Membership;
import ed.hbut.supermarket1.entity.Result;
import ed.hbut.supermarket1.service.MembershipService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class MembershipController {

    @Autowired
    private MembershipService membershipService;

    /**
     * 查询所有会员
     *
     */
    @GetMapping("/api/membership")
    public Result membership() {

        List<Membership> list=  membershipService.getAllMembership();

        return Result.success(list);
    }

    /**
     * 新增会员
     * @param membership
     * @return
     */
    @PostMapping("/api/membership")
    public Result addMembership(@RequestBody Membership membership) {

        log.info("新增会员 {}", membership);
        membershipService.addMembership(membership);

        return Result.success();
    }

    @GetMapping("/api/membership/{id}")
    public Result getMembership(@PathVariable int id) {
        Membership membership = membershipService.getById(id);
        return Result.success(membership);
    }

    @PutMapping("/api/membership/{id}")
    public Result updateMembership(@PathVariable int id, @RequestBody Membership membership) {
        log.info("修改会员id:{}",id);
        log.info(membership.toString());
        membershipService.updateMembership(id,membership);

        return Result.success();
    }

    @DeleteMapping("/api/membership/{id}")
    public Result deleteMembership(@PathVariable int id) {

        membershipService.deleteMembership(id);

        return Result.success();

    }


}
